'/*********************************************************************************
'*   (c) Copyright Kofax Image Products, 2006 All rights reserved.
'*   Unauthorized use, duplication or distribution is strictly prohibited.
'**********************************************************************************
'*
'*  File:       WorkflowAgentSetup.vb
'*
'*  Purpose:    The workflow agent setup dialog. Enables the user to select
'*              between manual validation, or skip validation if confidence
'*              value is equal or greater than the selected value.
'*
'**********************************************************************************/

Imports System.Runtime.InteropServices
Imports Kofax.AscentCaptureAdminMod
Imports Kofax.KHelpFinder
Imports System.Io

<GuidAttribute("A3418F65-D92B-3B61-A65F-2D8A2BD4AFB2"), _
ClassInterface(ClassInterfaceType.None), _
ProgId("VBNetSampleWorkflowSetup.WorkflowAgentSetup")> _
Friend Class AllStateBatchRouterWorkflowAgentSetup
    Inherits System.Windows.Forms.Form

    Private m_oApp As AdminApplication

    '*** Constants
    Private Const lDEFAULT_CONFIDENCE As Long = 95
    Private Const strCUSTOM_STORAGE_NAME As String = _
        "Kofax.ACWFSample.NET.Confidence"

    '*** This is a reference to a help file, and it may not work 
    '*** for everyone. Here is where the path to a help file
    '*** would go
    Private Const lHELP_ID As Long = 32871

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
    Friend WithEvents cmdOK As System.Windows.Forms.Button
    Friend WithEvents cmdCancel As System.Windows.Forms.Button
    Friend WithEvents cmdHelp As System.Windows.Forms.Button
    Friend WithEvents OptManuallyValidate As System.Windows.Forms.RadioButton
    Friend WithEvents OptSkipManual As System.Windows.Forms.RadioButton
    Friend WithEvents SliderConfidence As System.Windows.Forms.TrackBar
    Friend WithEvents lblConfidence As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.GroupBox1 = New System.Windows.Forms.GroupBox
        Me.lblConfidence = New System.Windows.Forms.Label
        Me.SliderConfidence = New System.Windows.Forms.TrackBar
        Me.OptSkipManual = New System.Windows.Forms.RadioButton
        Me.OptManuallyValidate = New System.Windows.Forms.RadioButton
        Me.cmdOK = New System.Windows.Forms.Button
        Me.cmdCancel = New System.Windows.Forms.Button
        Me.cmdHelp = New System.Windows.Forms.Button
        Me.GroupBox1.SuspendLayout()
        CType(Me.SliderConfidence, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'GroupBox1
        '
        Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblConfidence, Me.SliderConfidence, Me.OptSkipManual, Me.OptManuallyValidate})
        Me.GroupBox1.Location = New System.Drawing.Point(8, 8)
        Me.GroupBox1.Name = "GroupBox1"
        Me.GroupBox1.Size = New System.Drawing.Size(320, 144)
        Me.GroupBox1.TabIndex = 0
        Me.GroupBox1.TabStop = False
        Me.GroupBox1.Text = "Validation Behavior"
        '
        'lblConfidence
        '
        Me.lblConfidence.Location = New System.Drawing.Point(110, 112)
        Me.lblConfidence.Name = "lblConfidence"
        Me.lblConfidence.Size = New System.Drawing.Size(100, 16)
        Me.lblConfidence.TabIndex = 3
        Me.lblConfidence.Text = "95%"
        Me.lblConfidence.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'SliderConfidence
        '
        Me.SliderConfidence.Location = New System.Drawing.Point(8, 88)
        Me.SliderConfidence.Maximum = 100
        Me.SliderConfidence.Name = "SliderConfidence"
        Me.SliderConfidence.Size = New System.Drawing.Size(296, 45)
        Me.SliderConfidence.TabIndex = 2
        Me.SliderConfidence.TickStyle = System.Windows.Forms.TickStyle.None
        '
        'OptSkipManual
        '
        Me.OptSkipManual.Location = New System.Drawing.Point(8, 56)
        Me.OptSkipManual.Name = "OptSkipManual"
        Me.OptSkipManual.Size = New System.Drawing.Size(304, 24)
        Me.OptSkipManual.TabIndex = 1
        Me.OptSkipManual.Text = "&Skip validation if the confidence for all fields is at least"
        '
        'OptManuallyValidate
        '
        Me.OptManuallyValidate.Location = New System.Drawing.Point(8, 24)
        Me.OptManuallyValidate.Name = "OptManuallyValidate"
        Me.OptManuallyValidate.Size = New System.Drawing.Size(256, 24)
        Me.OptManuallyValidate.TabIndex = 0
        Me.OptManuallyValidate.Text = "&Always validate"
        '
        'cmdOK
        '
        Me.cmdOK.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.cmdOK.Location = New System.Drawing.Point(336, 16)
        Me.cmdOK.Name = "cmdOK"
        Me.cmdOK.Size = New System.Drawing.Size(72, 24)
        Me.cmdOK.TabIndex = 1
        Me.cmdOK.Text = "OK"
        '
        'cmdCancel
        '
        Me.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.cmdCancel.Location = New System.Drawing.Point(336, 48)
        Me.cmdCancel.Name = "cmdCancel"
        Me.cmdCancel.Size = New System.Drawing.Size(72, 24)
        Me.cmdCancel.TabIndex = 2
        Me.cmdCancel.Text = "Cancel"
        '
        'cmdHelp
        '
        Me.cmdHelp.Location = New System.Drawing.Point(336, 80)
        Me.cmdHelp.Name = "cmdHelp"
        Me.cmdHelp.Size = New System.Drawing.Size(72, 24)
        Me.cmdHelp.TabIndex = 3
        Me.cmdHelp.Text = "&Help"
        '
        'WorkflowAgentSetup
        '
        Me.AcceptButton = Me.cmdOK
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.CancelButton = Me.cmdCancel
        Me.ClientSize = New System.Drawing.Size(424, 166)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.GroupBox1, Me.cmdHelp, Me.cmdCancel, Me.cmdOK})
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "WorkflowAgentSetup"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Vaildation Workflow Properties"
        Me.GroupBox1.ResumeLayout(False)
        CType(Me.SliderConfidence, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region


    '**************************************************
    '*** Function: SetApp
    '*** Purpose:  Initializes the applicaiton object.
    '*** Input:    oApp - the application object
    '**************************************************
    Public Sub SetApp(ByVal oApp As AdminApplication)
        m_oApp = oApp
    End Sub

    '**************************************************
    '*** Function: cmdOK_Click
    '*** Purpose:  Save settings and hides the dialog.
    '**************************************************
    Private Sub cmdOK_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles cmdOK.Click
        ' Update Database with the current settings
        If OptManuallyValidate.Checked = True Then
            m_oApp.ActiveBatchClass.CustomStorageString _
                (strCUSTOM_STORAGE_NAME) = "-" & SliderConfidence.Value
        Else
            m_oApp.ActiveBatchClass.CustomStorageString _
                (strCUSTOM_STORAGE_NAME) = SliderConfidence.Value
        End If
        Me.Hide()
    End Sub

    '**************************************************
    '*** Function: cmdCancel_Click
    '*** Purpose:  Exits WorkflowAgent Setup dialog
    '***           and ignores changes.
    '**************************************************
    Private Sub cmdCancel_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.Hide()
    End Sub

    '**************************************************
    '*** Function: cmdHelp_Click
    '*** Purpose:  Calls the proprietary Ascent Capture 
    '***           help sysetm.
    '**************************************************
    Private Sub cmdHelp_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles cmdHelp.Click
        Dim oHelp As New KChmHlp
        Dim pbCancel As Boolean

        '*** Admin tells what the file name is
        oHelp.ShowHelp(Application.StartupPath + Path.DirectorySeparatorChar _
            + "Admin", "", lHELP_ID, 0, pbCancel)
    End Sub

    '**************************************************
    '*** Function: OptSkipManual_Click
    '*** Purpose:  Enables slider if skip manual validation
    '***           is selected.
    '*** Input:
    '*** Output:
    '**************************************************
    Private Sub OptSkipManual_CheckedChanged(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles OptSkipManual.CheckedChanged
        If OptSkipManual.Checked = True Then
            lblConfidence.Enabled = True
            SliderConfidence.Enabled = True
        Else
            lblConfidence.Enabled = False
            SliderConfidence.Enabled = False
        End If
    End Sub

    '**************************************************
    '*** Function: SliderConfidence_Scroll
    '*** Purpose:  Updates the confidence level
    '**************************************************
    Private Sub SliderConfidence_Scroll(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles SliderConfidence.Scroll
        lblConfidence.Text = SliderConfidence.Value & "%"
    End Sub

    '**************************************************
    '*** Function: WorkflowAgentSetup_Load
    '*** Purpose:  Load the form
    '**************************************************
    Private Sub WorkflowAgentSetup_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load

        '*** Initialize the data
        Dim strConfidence As String
        Try
            ' Set confidence level to default
            lblConfidence.Text = lDEFAULT_CONFIDENCE & "%"
            SliderConfidence.Value = lDEFAULT_CONFIDENCE
            lblConfidence.Enabled = False
            SliderConfidence.Enabled = False
            OptManuallyValidate.Checked = True
        Finally

            ' Read confidence level from database
            Try
                strConfidence = m_oApp.ActiveBatchClass.CustomStorageString _
                 (strCUSTOM_STORAGE_NAME)
            Catch
                ' If first time (CustomStorageString not in database),
                ' use the default values
                strConfidence = ""
            End Try

            ' Set confidence level based on the value read from the database
            If strConfidence <> "" Then
                Dim lConfidence As Long
                lConfidence = strConfidence
                lblConfidence.Text = Math.Abs(lConfidence) & "%"
                SliderConfidence.Value = Math.Abs(lConfidence)

                ' If confidence level not negative,
                ' enable skip manual validation.
                If lConfidence >= 0 Then
                    lblConfidence.Enabled = True
                    SliderConfidence.Enabled = True
                    OptSkipManual.Checked = True
                End If
            End If
        End Try
    End Sub
End Class
